// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html


import {ExcelConfig} from "./ExcelConfig";
import Tool from "./Tool";
const {ccclass, property} = cc._decorator;

const JsonName = "ballData_full";

const commodityPrice = 250;

@ccclass
export default class shopConfig {

    static shopUserMessage = [];

    static shopAllMessage = [];

    static shopUsableMessage = [];

    static userMoney = 250;

    //获取用户商城本地信息
    static getUserLocalMessage(){
        let  data = Tool.getData("shopUserMessage");
        if(data == null || data == "" || data.length == 0){
            shopConfig.shopUserMessage = [{id:1,pic:"01.png"}];
        }else{
            shopConfig.shopUserMessage = JSON.parse(data);
        }
        console.log("用户拥有商品信息=========>",shopConfig.shopUserMessage);
        return shopConfig.shopUserMessage;
    }

    //存储用户商城本地信息
    static saveUserLocalMessage(){
        let saveData = JSON.stringify(shopConfig.shopUserMessage);
        Tool.saveData("shopUserMessage",saveData);
    }

    //获取用户未拥有的商城本地信息
    static getShopLocalMessage(){
        let  data = Tool.getData("shopUsableMessage");
        if(data == null || data == "" || data.length == 0){
            shopConfig.shopUsableMessage = shopConfig.shopAllMessage;
        }else{
            shopConfig.shopUsableMessage = JSON.parse(data);
        }
        console.log("用户的未购买的商品信息====>",shopConfig.shopUsableMessage);
    }

    //存储用户未拥有的商城本地信息
    static saveShopLocalMessage(){
        let saveData = JSON.stringify(shopConfig.shopUsableMessage);
        Tool.saveData("shopUsableMessage",saveData);
    }

    //将存在Json文件中的商城信息 转成可用信息  该方法先于getShopMessage调用
    static changeShopMessage(){
        ExcelConfig.loadExcel("Data/" + JsonName);
    }

    //获取存在Json文件中的商城信息
    static getShopMessage(){
        let data = ExcelConfig.getExcelTable("Data/" + JsonName);
        shopConfig.shopAllMessage = data;
        console.log("商城所有商品信息====>",shopConfig.shopAllMessage);
    }

    //获取人物的钱
    static getUserMoney(){
        let data = Tool.getData("userMoney");
        if(data == "" || data == null){
            shopConfig.userMoney = 250;
        }else{
            shopConfig.userMoney = parseInt(data);
        }
    }

    //存储用户的钱
    static saveUserMoney(){
        Tool.saveData("userMoney",shopConfig.userMoney);
    }

    static showMoney(node){
        node.getComponent(cc.Label).string = shopConfig.userMoney;
    }

    //扣钱
    static deductMoney(callback){
        if(shopConfig.userMoney - commodityPrice < 0){
            callback(false);
        }else{
            shopConfig.userMoney -= commodityPrice;
            shopConfig.saveUserMoney();
            callback(true);
        }
    }

    //随机解锁一个商品
    static randomUnlockOne(){
        let random = Math.random() * this.shopUsableMessage.length | 0;
        let randomTarget = this.shopUsableMessage[random];
        this.shopUserMessage.push(randomTarget);
        if(this.shopUsableMessage.length == 0){
            console.log("商品卖完=======>");
            return;
        }
        this.shopUsableMessage.splice(random,1);
        shopConfig.saveShopLocalMessage();
        shopConfig.saveUserLocalMessage();
        return randomTarget;
    }

    //随机解锁多个商品
    static randomUnlockMore(unlock){
        if(unlock > this.shopUsableMessage.length){
            unlock = this.shopUsableMessage.length;
            console.log("解锁商品个数超过可解锁的商品个数");
        }
        let randomArr = this.generatedRandomNumbers(unlock,[0,this.shopUsableMessage.length]);
        let unlockArr = [];
        for(let i in randomArr){
            unlockArr.push(this.shopUsableMessage[randomArr[i]]);
            this.shopUserMessage.push(this.shopUsableMessage[randomArr[i]]);
        }
        return unlockArr;
    }


    static randomNumbers(arr,range){
        let randomNumber = (range[0] + Math.random() * (range[1] -  range[0])) | 0;
        if(arr.indexOf(randomNumber) < 0){
            arr.push(randomNumber);
        }else{
            this.randomNumbers(arr,range);
        }
    }

    static generatedRandomNumbers(length,range){
        let array = [];
        for(let i = 0;i < length;i++){
            this.randomNumbers(array,range);
        }
        return array;
    };


}
